<?php

namespace app\api\controller;

use app\admin\model\RiderOrder;
use app\common\controller\Api;
use app\admin\model\Order as Orders;
use app\common\exception\UploadException;
use app\common\library\Upload;
use think\Config;
use think\Db;

/**
 * 首页接口
 */
class Order extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $user = $this->auth->getUserinfo();
        $user_id = $user['id'];
        $page = (int)$this->request->post('page',1);
        $status = (int)$this->request->post('status',1);  //状态，0待揽件，1待配送，2配送成功, 9配送失败
        if(!in_array($status,[1,2,9])){
            return $this->error("参数不对");
        }
        $size = 10;
        $model = new \app\admin\model\Order();
        $list = $model->where(['status'=>$status,'rider_uid'=>$user_id])->page($page,$size)->select();
        if($list){
            foreach ($list as &$val){
                $val['button_list']=[
                    "whatsUp"=>'whatsUp'
                ];;
                if($val['status']==1){
                    $val['button_list']=[
                        'confirm'=>"送达状态",
                        "phone"=>"打电话",
                        "address"=>"复制地址",
                        "whatsUp"=>'whatsUp'
                       // "no_confirm"=>"送达失败"
                    ];
                }
                if($val['status']==9){
                    $val['button_list']=[
                        'confirm'=>"确认送达",
                        "phone"=>"打电话",
                        "address"=>"复制地址",
                        "whatsUp"=>'whatsUp'
                    ];
                }
            }
        }

        $this->success('请求成功',$list);
    }

    //揽件
    public function scanOrder(){
        $orderSn = $this->request->post('orderSn');
        $type = $this->request->post('type');
        if(empty($orderSn)){
            $this->error("缺少参数");
        }
        $model = new \app\admin\model\Order();
        $info = $model->where(['orderSn'=>$orderSn])->find();
        if(empty($info)){
            $this->error("包裹不存在");
        }
        if($info['status'] != 0){
            $this->error("包裹已经被揽件");
        }
        $user = $this->auth->getUserinfo();
        $user_id = $user['id'];

        if($type=='save'){
            Db::startTrans();
            try{
                Orders::update(['status'=>1,'rider_uid'=>$user_id],['orderSn'=>$orderSn]);
                $rider_order = new RiderOrder();
                $res = $rider_order->data([
                    "orderSn"=>$orderSn,
                    "user_id"=>$user_id,
                    "status"=>1,
                    "add_time"=>time()
                ])->save();
                if(empty($res)){
                    throw new \Exception("揽件失败");
                }
                Db::commit();
            }catch (\Exception $e){
                Db::rollback();

                $this->error($e->getMessage());
            }
            $this->success("揽件成功");
        }


        $this->success("ok",$info);
    }


    //确认送达
    public function confirmOrder(){
        $user = $this->auth->getUserinfo();
        $user_id = $user['id'];
        $orderSn = $this->request->post('orderSn');
        $remark = $this->request->post('remark');
        $thumbs = $this->request->post('thumbs');
        $model = new \app\admin\model\Order();
        $order = $model->where(['orderSn'=>$orderSn,'rider_uid'=>$user_id])->find();
        if(empty($order)){
            $this->error('数据不存在');
        }
        if(!in_array($order['status'],[1,9])){
            $this->error("该包裹已经送达");
        }

        Orders::update(['status'=>2,'end_thumbs'=>$thumbs,'remark'=>$remark],['orderSn'=>$orderSn]);
        RiderOrder::update(['status'=>2],['orderSn'=>$orderSn,'user_id'=>$user_id]);
        $this->success("确认成功");
    }


    //确认失败
    public function confirmFalse(){
        $user = $this->auth->getUserinfo();
        $user_id = $user['id'];
        $orderSn = $this->request->post('orderSn');
        $remark = $this->request->post('remark');
        $thumbs = $this->request->post('thumbs');
        $model = new \app\admin\model\Order();
        $order = $model->where(['orderSn'=>$orderSn,'rider_uid'=>$user_id])->find();
        if(empty($order)){
            $this->error('数据不存在');
        }

        if(!in_array($order['status'],[1,9])){
            $this->error("该包裹已经送达");
        }

        Orders::update(['status'=>9,'remark'=>$remark,'end_thumbs'=>$thumbs],['orderSn'=>$orderSn]);
        RiderOrder::update(['status'=>9],['orderSn'=>$orderSn,'user_id'=>$user_id]);
        $this->success("确认成功");
    }


    /**
     * 上传文件
     */
    public function upload()
    {
        Config::set('default_return_type', 'json');
        //必须设定cdnurl为空,否则cdnurl函数计算错误
        Config::set('upload.cdnurl', '');
        $attachment = null;
        //默认普通上传文件
        $file = $this->request->file('file');
        $url = "";
        try {
            $upload = new Upload($file);
            $attachment = $upload->upload();
            $url = $attachment->url;
        } catch (UploadException $e) {
            $this->error($e->getMessage());
        }
        $data = ['get'=>$_GET,'post'=>$_POST,'files'=>$_FILES,'file'=>$file,'url'=>$url];
        p($data);die;
    }




}
